package 中等.两数相加;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author liudingding
 * @ClassName Solution
 * @description
 * @date 2021/11/27 10:32 下午
 */
public class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1.next == null || l2.next == null){
            int sum = l1.val + l2.val;
            return new ListNode(sum);
        }
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        while (true) {
            sb1.append((l1.val));
            l1 = l1.next;
            if (l1.next == null) {
                sb1.append((l1.val));
                break;
            }
        }
        while (true) {
            sb2.append((l2.val));
            l2 = l2.next;
            if (l2.next == null) {
                sb2.append((l2.val));
                break;
            }
        }
        int i1 = Integer.parseInt(sb1.toString());
        int i2 = Integer.parseInt(sb2.toString());
        String s1 = new StringBuffer(String.valueOf(i1)).reverse().toString();
        String s2 = new StringBuffer(String.valueOf(i2)).reverse().toString();
        StringBuffer reverse = new StringBuffer(String.valueOf((Integer.parseInt(s1) + Integer.parseInt(s2)))).reverse();
        char[] chars = reverse.toString().toCharArray();
        ListNode newNode = new ListNode(0);
        ListNode tempNode = newNode;
        for (char aChar : chars) {
            int i = Integer.parseInt(String.valueOf(aChar));
            ListNode a = new ListNode(i);
            tempNode.next = a;
            tempNode = a;

        }
        return newNode.next;
    }

    public ListNode changeArrayToNode(int[] arr) {
        if(arr == null || arr.length <= 0){return null;}

        ListNode sentineNode = new ListNode(0);  //建立哨兵节点
        ListNode tempNode = sentineNode;  //tempNode变量指向哨兵节点
        for(int i = 0; i < arr.length; i++) {
            ListNode newNode = new ListNode(arr[i]);
            tempNode.next = newNode;    //临时节点‘后继指针’指向新节点
            tempNode = newNode;        //把新节点赋给临时变量tempNode
        }
        return sentineNode.next;
    }

    @Test
    public void test(){
        ListNode listNode1 = new ListNode(9);
        ListNode listNode2 = new ListNode(9);
        ListNode listNode3 = new ListNode(1);
//        ListNode listNode4 = new ListNode(9);
//        ListNode listNode5 = new ListNode(9);
//        ListNode listNode6 = new ListNode(9);
//        ListNode listNode7 = new ListNode(9);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
//        listNode3.next = listNode4;
//        listNode4.next = listNode5;
//        listNode5.next = listNode6;
//        listNode6.next = listNode7;

        ListNode listNode8 = new ListNode(1);
//        ListNode listNode9 = new ListNode(6);
//        ListNode listNode10 = new ListNode(4);
//        ListNode listNode11 = new ListNode(9);
//        listNode8.next = listNode9;
//        listNode9.next = listNode10;
//        listNode10.next = listNode11;
        System.out.println(addTwoNumbers(listNode1, listNode8));
    }

    @Test
    public void test2(){
        int[] a = {1,2};
        System.out.println(changeArrayToNode(a));
    }
}
